package etilan.Client;
import java.io.IOException;
import android.util.Log;
import etilan.Client.ButtonView.layout;
import etilan.Client.Connectors.Conn;
import etilan.Client.Connectors.bluetoothConn;
import etilan.Client.Connectors.wifiConn;

public class ConnManager implements Runnable {
	private long interval = 50;
	private String mode;
	private MainService parent;
	private Conn conn;
	private boolean invX = false, invY = false;

	public ConnManager(MainService droidPadServer, int interval2, String Mode, boolean invX, boolean invY){
		parent = droidPadServer;
		interval = (long) (float) ((1 / (float) interval2) * 1000);
		this.invX = invX;
		this.invY = invY;
		mode = Mode;
		Log.v("DroidPad", "DPC: Infos recieved (initiated)");
		String connMode=parent.prefs.getString("conntype", "wifi");
		if (connMode.equals("wifi")){
			int port=Integer.parseInt((parent.prefs.getString("portnumber", "3141")));
			String ip=parent.prefs.getString("ipaddress", "10.0.0.4");
			conn=new wifiConn(port,ip);
		}
		else
			conn=new bluetoothConn(parent.prefs.getString("bluetoothAddress", ""));
		
		//TODO: check that wifi/bluetooth are on before connecting
	}
	
	@Override
	public void run(){
		Log.d("DroidPad", "DPC: Thread started. Waiting for connection...");
		setup();
		float[] AVals;
		String str = "";
		int i;
		layout buttons;
		while(!conn.isStopping()){
			if(conn.socketOn()){
				try{
					AVals = parent.getAVals();
					str = "[{" + (invX ? AVals[0] : -AVals[0]) + "," + (invY ? AVals[1] : -AVals[1]) + "," + AVals[2] + "}";
					buttons = parent.getButtons();
					if(buttons != null){
						for(i = 0; i < buttons.items.length; i++)
						{
							str += ";";
							if(
									buttons.items[i].item == ButtonView.itemType.button ||
									buttons.items[i].item == ButtonView.itemType.toggle
									)
								str += buttons.items[i].pressed ? "1" : "0";
							else if(
									buttons.items[i].item == ButtonView.itemType.axis
									)
								str += "{A" + buttons.items[i].axisX + "," + -buttons.items[i].axisY + "}";
							else if(
									buttons.items[i].item == ButtonView.itemType.sliderX
									)
								str += "{S" + buttons.items[i].axisX + "}";
							else if(
									buttons.items[i].item == ButtonView.itemType.sliderY
									)
								str += "{S" + -buttons.items[i].axisY + "}";
						}
					}
					str += "]\n"; // [] for easy string view
					conn.write(str.getBytes());
					try {
						Thread.sleep(interval);
					} 
					catch (InterruptedException e) {e.printStackTrace();}
					conn.read();
				} 
				  catch (NumberFormatException e) {e.printStackTrace();}
				  catch (IOException e) {e.printStackTrace();}
			}
		}
	}
	
	private void setup(){
		try{
			conn.connInit();
			conn.write(("<MODE>" + mode + "</MODE>\n").getBytes());
		}
		catch(IOException e){
			e.printStackTrace();
			Log.e("DroidPad", "DPC: Error sending info to PC");
		}
	}
	
	public final synchronized void killThread(){
		conn.kill();
	}
	
	public final synchronized void changeParentWindow(MainActivity a){
		conn.changeParentWindow(a);
	}
}